{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "------------------------------------------------------------------------------------\n",
      "      Linora\n",
      "--------------------\n",
      "      Version      : -- 0.6.0  --\n",
      "      Author       : JinQing Lee\n",
      "      License      : Apache-2.0\n",
      "      Homepage     : https://github.com/Hourout/linora\n",
      "      Description  : Simple and efficient tools for data mining and data analysis.\n",
      "------------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "import linora as la\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "la._hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>feature</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-6</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-7</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>-6</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>-8</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>-9</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>-7</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>-8</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>-9</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>-7</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>-10</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    feature  label\n",
       "0        -6      0\n",
       "1         9      1\n",
       "2         3      1\n",
       "3        -7      0\n",
       "4        -2      0\n",
       "5         0      1\n",
       "6         0      1\n",
       "7         0      1\n",
       "8         8      1\n",
       "9        -6      0\n",
       "10       -8      0\n",
       "11       -9      0\n",
       "12        0      0\n",
       "13        2      1\n",
       "14       -7      1\n",
       "15       -8      0\n",
       "16        2      1\n",
       "17       -9      0\n",
       "18        1      0\n",
       "19       -7      0\n",
       "20      -10      0"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = pd.DataFrame({'feature':np.random.randint(-10, 10, 21), 'label':np.random.randint(0,2,21)})\n",
    "t"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# K-fold\n",
    "### K-fold cv with no shuffle and stratify."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 fold, train index [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], test index [0, 1, 2, 3, 4, 5, 6]\n",
      "1 fold, train index [0, 1, 2, 3, 4, 5, 6, 14, 15, 16, 17, 18, 19, 20], test index [7, 8, 9, 10, 11, 12, 13]\n",
      "2 fold, train index [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], test index [14, 15, 16, 17, 18, 19, 20]\n"
     ]
    }
   ],
   "source": [
    "index = la.sample_splits.kfold(t, n_splits=3)\n",
    "for i, j in enumerate(index):\n",
    "    print(f'{i} fold, train index {j[0]}, test index {j[1]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### K-fold cv with shuffle but no stratify."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 fold, train index [16, 14, 17, 15, 1, 19, 0, 11, 2, 7, 8, 9, 4, 18], test index [12, 10, 6, 13, 20, 5, 3]\n",
      "1 fold, train index [12, 10, 6, 13, 20, 5, 3, 11, 2, 7, 8, 9, 4, 18], test index [16, 14, 17, 15, 1, 19, 0]\n",
      "2 fold, train index [12, 10, 6, 13, 20, 5, 3, 16, 14, 17, 15, 1, 19, 0], test index [11, 2, 7, 8, 9, 4, 18]\n"
     ]
    }
   ],
   "source": [
    "index = la.sample_splits.kfold(t, n_splits=3, shuffle=True)\n",
    "for i, j in enumerate(index):\n",
    "    print(f'{i} fold, train index {j[0]}, test index {j[1]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### K-fold cv with stratify but no shuffle."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 fold, train index [10, 11, 12, 15, 6, 7, 8, 17, 18, 19, 20, 13, 14, 16], test index [0, 3, 4, 9, 1, 2, 5]\n",
      "1 fold, train index [0, 3, 4, 9, 1, 2, 5, 17, 18, 19, 20, 13, 14, 16], test index [10, 11, 12, 15, 6, 7, 8]\n",
      "2 fold, train index [0, 3, 4, 9, 1, 2, 5, 10, 11, 12, 15, 6, 7, 8], test index [17, 18, 19, 20, 13, 14, 16]\n"
     ]
    }
   ],
   "source": [
    "index = la.sample_splits.kfold(t, stratify=t.label, n_splits=3, shuffle=False)\n",
    "for i, j in enumerate(index):\n",
    "    print(f'{i} fold, train index {j[0]}, test index {j[1]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### K-fold cv with shuffle and stratify."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 fold, train index [17, 4, 11, 19, 1, 16, 6, 18, 15, 3, 9, 8, 14, 13], test index [0, 20, 10, 12, 2, 5, 7]\n",
      "1 fold, train index [0, 20, 10, 12, 2, 5, 7, 18, 15, 3, 9, 8, 14, 13], test index [17, 4, 11, 19, 1, 16, 6]\n",
      "2 fold, train index [0, 20, 10, 12, 2, 5, 7, 17, 4, 11, 19, 1, 16, 6], test index [18, 15, 3, 9, 8, 14, 13]\n"
     ]
    }
   ],
   "source": [
    "index = la.sample_splits.kfold(t, stratify=t.label, n_splits=3, shuffle=True)\n",
    "for i, j in enumerate(index):\n",
    "    print(f'{i} fold, train index {j[0]}, test index {j[1]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# train_test_split\n",
    "\n",
    "### train_test_split with no shuffle and stratify."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train index [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], \n",
      "test index  [17, 18, 19, 20]\n",
      "trainset\n",
      "     feature  label\n",
      "0        -6      0\n",
      "1         9      1\n",
      "2         3      1\n",
      "3        -7      0\n",
      "4        -2      0\n",
      "5         0      1\n",
      "6         0      1\n",
      "7         0      1\n",
      "8         8      1\n",
      "9        -6      0\n",
      "10       -8      0\n",
      "11       -9      0\n",
      "12        0      0\n",
      "13        2      1\n",
      "14       -7      1\n",
      "15       -8      0\n",
      "16        2      1\n",
      "testset\n",
      "     feature  label\n",
      "17       -9      0\n",
      "18        1      0\n",
      "19       -7      0\n",
      "20      -10      0\n"
     ]
    }
   ],
   "source": [
    "index = la.sample_splits.train_test_split(t, test_size=0.2)\n",
    "print(f'train index {index[0]}, \\ntest index  {index[1]}')\n",
    "print('trainset\\n',t.loc[index[0]])\n",
    "print('testset\\n',t.loc[index[1]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### train_test_split with shuffle but no stratify."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train index [11, 8, 14, 7, 15, 16, 19, 18, 13, 3, 0, 17, 10, 4, 20, 1, 5], \n",
      "test index  [2, 6, 9, 12]\n",
      "trainset\n",
      "     feature  label\n",
      "11       -9      0\n",
      "8         8      1\n",
      "14       -7      1\n",
      "7         0      1\n",
      "15       -8      0\n",
      "16        2      1\n",
      "19       -7      0\n",
      "18        1      0\n",
      "13        2      1\n",
      "3        -7      0\n",
      "0        -6      0\n",
      "17       -9      0\n",
      "10       -8      0\n",
      "4        -2      0\n",
      "20      -10      0\n",
      "1         9      1\n",
      "5         0      1\n",
      "testset\n",
      "     feature  label\n",
      "2         3      1\n",
      "6         0      1\n",
      "9        -6      0\n",
      "12        0      0\n"
     ]
    }
   ],
   "source": [
    "index = la.sample_splits.train_test_split(t, test_size=0.2, shuffle=True)\n",
    "print(f'train index {index[0]}, \\ntest index  {index[1]}')\n",
    "print('trainset\\n',t.loc[index[0]])\n",
    "print('testset\\n',t.loc[index[1]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### train_test_split with stratify but no shuffle."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train index [0, 3, 4, 9, 10, 11, 12, 15, 17, 18, 1, 2, 5, 6, 7, 8, 13], \n",
      "test index  [19, 20, 14, 16]\n",
      "trainset\n",
      "     feature  label\n",
      "0        -6      0\n",
      "3        -7      0\n",
      "4        -2      0\n",
      "9        -6      0\n",
      "10       -8      0\n",
      "11       -9      0\n",
      "12        0      0\n",
      "15       -8      0\n",
      "17       -9      0\n",
      "18        1      0\n",
      "1         9      1\n",
      "2         3      1\n",
      "5         0      1\n",
      "6         0      1\n",
      "7         0      1\n",
      "8         8      1\n",
      "13        2      1\n",
      "testset\n",
      "     feature  label\n",
      "19       -7      0\n",
      "20      -10      0\n",
      "14       -7      1\n",
      "16        2      1\n"
     ]
    }
   ],
   "source": [
    "index = la.sample_splits.train_test_split(t, stratify=t.label, test_size=0.2, shuffle=False)\n",
    "print(f'train index {index[0]}, \\ntest index  {index[1]}')\n",
    "print('trainset\\n',t.loc[index[0]])\n",
    "print('testset\\n',t.loc[index[1]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### train_test_split with shuffle and stratify."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train index [1, 2, 5, 8, 14, 16, 7, 20, 3, 9, 15, 0, 18, 10, 11, 17, 19], \n",
      "test index  [6, 13, 12, 4]\n",
      "trainset\n",
      "     feature  label\n",
      "1         9      1\n",
      "2         3      1\n",
      "5         0      1\n",
      "8         8      1\n",
      "14       -7      1\n",
      "16        2      1\n",
      "7         0      1\n",
      "20      -10      0\n",
      "3        -7      0\n",
      "9        -6      0\n",
      "15       -8      0\n",
      "0        -6      0\n",
      "18        1      0\n",
      "10       -8      0\n",
      "11       -9      0\n",
      "17       -9      0\n",
      "19       -7      0\n",
      "testset\n",
      "     feature  label\n",
      "6         0      1\n",
      "13        2      1\n",
      "12        0      0\n",
      "4        -2      0\n"
     ]
    }
   ],
   "source": [
    "index = la.sample_splits.train_test_split(t, stratify=t.label, test_size=0.2, shuffle=True)\n",
    "print(f'train index {index[0]}, \\ntest index  {index[1]}')\n",
    "print('trainset\\n',t.loc[index[0]])\n",
    "print('testset\\n',t.loc[index[1]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# timeseries_kfold\n",
    "\n",
    "### timeseries_kfold with gap=0."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 fold, train index [0, 1, 2, 3, 4, 5], test index [6, 7]\n",
      "1 fold, train index [2, 3, 4, 5, 6, 7], test index [8, 9]\n",
      "2 fold, train index [4, 5, 6, 7, 8, 9], test index [10, 11]\n",
      "3 fold, train index [6, 7, 8, 9, 10, 11], test index [12, 13]\n",
      "4 fold, train index [8, 9, 10, 11, 12, 13], test index [14, 15]\n",
      "5 fold, train index [10, 11, 12, 13, 14, 15], test index [16, 17]\n",
      "6 fold, train index [12, 13, 14, 15, 16, 17], test index [18, 19]\n"
     ]
    }
   ],
   "source": [
    "index = la.sample_splits.timeseries_kfold(t, train_size=0.3, test_size=0.1, gap=0, ascending=True)\n",
    "for i, j in enumerate(index):\n",
    "    print(f'{i} fold, train index {j[0]}, test index {j[1]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### timeseries_kfold with gap>0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 fold, train index [0, 1, 2, 3, 4, 5], test index [8, 9]\n",
      "1 fold, train index [2, 3, 4, 5, 6, 7], test index [10, 11]\n",
      "2 fold, train index [4, 5, 6, 7, 8, 9], test index [12, 13]\n",
      "3 fold, train index [6, 7, 8, 9, 10, 11], test index [14, 15]\n",
      "4 fold, train index [8, 9, 10, 11, 12, 13], test index [16, 17]\n",
      "5 fold, train index [10, 11, 12, 13, 14, 15], test index [18, 19]\n"
     ]
    }
   ],
   "source": [
    "index = la.sample_splits.timeseries_kfold(t, train_size=0.3, test_size=0.1, gap=2, ascending=True)\n",
    "for i, j in enumerate(index):\n",
    "    print(f'{i} fold, train index {j[0]}, test index {j[1]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# timeseries_train_test_split\n",
    "\n",
    "### timeseries_train_test_split with gap=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train index [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], \n",
      "test index  [17, 18, 19, 20]\n"
     ]
    }
   ],
   "source": [
    "index = la.sample_splits.timeseries_train_test_split(t, test_size=0.2, gap=0, ascending=True)\n",
    "print(f'train index {index[0]}, \\ntest index  {index[1]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### timeseries_train_test_split with gap>0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train index [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], \n",
      "test index  [17, 18, 19, 20]\n"
     ]
    }
   ],
   "source": [
    "index = la.sample_splits.timeseries_train_test_split(t, test_size=0.2, gap=2, ascending=True)\n",
    "print(f'train index {index[0]}, \\ntest index  {index[1]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# timeseries_walk_forward_fold\n",
    "\n",
    "### timeseries_walk_forward_fold with gap=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 fold, train index [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], test index [16, 17, 18, 19, 20]\n",
      "1 fold, train index [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], test index [11, 12, 13, 14, 15]\n",
      "2 fold, train index [0, 1, 2, 3, 4, 5], test index [6, 7, 8, 9, 10]\n"
     ]
    }
   ],
   "source": [
    "index = la.sample_splits.timeseries_walk_forward_fold(t, n_splits=3, gap=0, ascending=True)\n",
    "for i, j in enumerate(index):\n",
    "    print(f'{i} fold, train index {j[0]}, test index {j[1]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### timeseries_walk_forward_fold with gap>0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 fold, train index [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], test index [16, 17, 18, 19, 20]\n",
      "1 fold, train index [0, 1, 2, 3, 4, 5, 6, 7, 8], test index [11, 12, 13, 14, 15]\n",
      "2 fold, train index [0, 1, 2, 3], test index [6, 7, 8, 9, 10]\n"
     ]
    }
   ],
   "source": [
    "index = la.sample_splits.timeseries_walk_forward_fold(t, n_splits=3, gap=2, ascending=True)\n",
    "for i, j in enumerate(index):\n",
    "    print(f'{i} fold, train index {j[0]}, test index {j[1]}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
